package com.example.demo.controller;

import com.example.demo.entity.Designer;
import com.example.demo.service.DesignerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/designer")
public class DesignerController {
    @Resource
    DesignerService designerService;

    @RequestMapping("/index")
    public String index(HttpSession session){
        Designer designer =(Designer)(session.getAttribute("designer"));
        if (designer==null) {
            return "login";
        }
        return "ndex";
    }


    @RequestMapping(value = "/modifyPwd",method = RequestMethod.GET)
    public String modifyPwd(HttpSession session){
        Designer designer =(Designer)(session.getAttribute("designer"));
        if (designer==null) {
            return "login";
        }
        return "modify_pwd";
    }

    @RequestMapping(value = "/modifyPwd",method = RequestMethod.POST)
    @ResponseBody
    public Map modifyPwd(String password,String newPwd,HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        Map map = designerService.modifyPwd(password, newPwd, designer.getDesId());
        return map;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Integer login(Designer designer, HttpSession session){
        Map map = designerService.login(designer);
        Integer msg = Integer.parseInt(map.get("msg").toString());
        if(msg==2){
            session.setAttribute("designer",map.get("designer"));

        }
        return msg;
    }

    @RequestMapping("/getDesId")
    @ResponseBody
    public Designer getDesId(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        return designer;
    }

    @RequestMapping("/getDesignerById")
    @ResponseBody
    public Designer getDesignerById(String desId){
        Designer designer = designerService.getDesignerByDesId(desId);
        return designer;
    }
}
